<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="18" :offset="3">
                <el-card shadow="always">
                    <el-row :gutter="20">
                        <!-- 头像 -->
                        <el-col :span="4" :offset="10" style="margin-left:50%;">
                            <i class="el-icon-user-solid" />
                        </el-col>
                    </el-row>
                    <br><br><br>
                    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                        class="demo-ruleForm">
                        <el-row :gutter="20">
                            <el-col :span="6">
                                <el-select v-model="ruleForm.isOther">
                                    <el-option label="用户" value="0"></el-option>
                                    <el-option label="员工" value="1"></el-option>
                                </el-select>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="昵称" prop="username">
                                    <el-input placeholder="请输入内容" v-model="ruleForm.username">
                                    </el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12" :offset="6">
                                <el-form-item label="身份证号" prop="idNumber">
                                    <el-input placeholder="请输入内容" v-model="ruleForm.idNumber"
                                        onkeyup="this.value=this.value.replace(/[^\X0-9]/g, '')">
                                    </el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12" :offset="6">
                                <el-form-item label="姓名" prop="name">
                                    <el-input placeholder="请输入内容" v-model="ruleForm.name">
                                    </el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12" :offset="6">
                                <el-form-item label="手机号" prop="phone">
                                    <el-input placeholder="请输入内容" v-model="ruleForm.phone">
                                    </el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12" :offset="6">
                                <el-form-item label="性别">
                                    <el-radio-group v-model="ruleForm.sex">
                                        <el-radio label="1">男</el-radio>
                                        <el-radio label="0">女</el-radio>
                                    </el-radio-group>
                                </el-form-item>
                            </el-col>
                            
                            <el-col :span="12" :offset="6">
                                <el-form-item label="密码" prop="password">
                                    <el-input placeholder="请输入密码" v-model="ruleForm.password" autocomplete="off" />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12" :offset="6">
                                <el-form-item label="确认密码" prop="checkPassword">
                                    <el-input placeholder="请输入密码" v-model="ruleForm.checkPassword" autocomplete="off" />
                                </el-form-item>
                            </el-col>
                            <el-col :span="4" :offset="10" style="margin-left:40%">
                                <el-form-item>
                                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </el-card>
            </el-col>
        </el-row>

    </div>
</template>
<script>
import { enroll } from '@/utils/requests.js'

export default {
    mounted(){
        this.init()
    },
    data() {
        // 验证手机号
        var validateMobilePhone = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('负责人手机号不可为空'));
            } else {
                let reg = new RegExp(/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/);
                if (!reg.test(value)) {
                    callback(new Error('请输入有效的手机号码'));
                    return;
                }
                callback();
            }
        }
        //          验证身份证号
        var checkIdentitytionId = (rule, value, callback) => {
            if (!value) {
                return callback(new Error("身份证号不能为空"));
            }
            if (!/(^\d{15}$)|(^\d{17}(\d|X|x)$)/.test(value)) {
                callback(new Error("你输入的身份证长度或格式错误"));
            }
            //身份证城市
            var aCity = {
                11: "北京",
                12: "天津",
                13: "河北",
                14: "山西",
                15: "内蒙古",
                21: "辽宁",
                22: "吉林",
                23: "黑龙江",
                31: "上海",
                32: "江苏",
                33: "浙江",
                34: "安徽",
                35: "福建",
                36: "江西",
                37: "山东",
                41: "河南",
                42: "湖北",
                43: "湖南",
                44: "广东",
                45: "广西",
                46: "海南",
                50: "重庆",
                51: "四川",
                52: "贵州",
                53: "云南",
                54: "西藏",
                61: "陕西",
                62: "甘肃",
                63: "青海",
                64: "宁夏",
                65: "新疆",
                71: "台湾",
                81: "香港",
                82: "澳门",
                91: "国外"
            }

            if (!aCity[parseInt(value.substr(0, 2))]) {
                callback(new Error("你的身份证地区非法"))
            }
            // 出生日期验证
            var sBirthday = (
                value.substr(6, 4) +
                "-" +
                Number(value.substr(10, 2)) +
                "-" +
                Number(value.substr(12, 2))
            ).replace(/-/g, "/"),
                d = new Date(sBirthday);
            if (
                sBirthday !=
                d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate()
            ) {
                callback(new Error("身份证上的出生日期非法"))
            }

            // 身份证号码校验
            var sum = 0,
                weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
                codes = "10X98765432"
            for (var i = 0; i < value.length - 1; i++) {
                sum += value[i] * weights[i]
            }
            var last = codes[sum % 11] //计算出来的最后一位身份证号码
            if (value[value.length - 1] != last) {
                callback(new Error("你输入的身份证号非法"))
            }
            callback()
        }
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'))
            } else {
                if (this.ruleForm.checkPassword !== '') {
                    this.$refs.ruleForm.validateField('checkPassword');
                }
                callback();
            }
        }
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'))
            } else if (value !== this.ruleForm.password) {
                callback(new Error('两次输入密码不一致!'))
            } else {
                callback();
            }
        }
        return {
            ruleForm: {
                password: '',
                checkPassword: '',
                idNumber: '',
                sex: '0',
                username: '',
                name: '',
                phone: '',
                isOther: '0'

            },
            rules: {
                password: [
                    { validator: validatePass, trigger: 'blur' }
                ],
                checkPassword: [
                    { validator: validatePass2, trigger: 'blur' }
                ],
                username: [
                    { required: true, message: '请输入昵称' },
                    { min: 1, max: 20, message: '长度在 1 到 20 个字符之间', trigger: 'blur' }
                ],
                idNumber: [
                    { validator: checkIdentitytionId, trigger: 'blur' }
                ],
                sex: [
                    { required: true, message: '请选择您的性别', trigger: 'change' }
                ],
                name: [
                    { required: true, message: '请输入你的姓名' }
                ],
                phone: [
                    { validator: validateMobilePhone, trigger: 'blur' }
                ]

            },
        }
    },
    methods: {
        init() {
            !function () {
                function o(w, v, i) {
                    return w.getAttribute(v) || i
                }
                function j(i) {
                    return document.getElementsByTagName(i)
                }
                function l() {
                    var i = j("script"),
                        w = i.length,
                        v = i[w - 1];
                    return {
                        l: w,
                        z: o(v, "zIndex", -1),
                        o: o(v, "opacity", 0.5),
                        c: o(v, "color", "0,0,0"),
                        n: o(v, "count", 99)
                    }
                }
                function k() {
                    r = u.width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
                        n = u.height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
                }
                function b() {
                    e.clearRect(0, 0, r, n);
                    var w = [f].concat(t);
                    var x, v, A, B, z, y;
                    t.forEach(function (i) {
                        i.x += i.xa,
                            i.y += i.ya,
                            i.xa *= i.x > r || i.x < 0 ? -1 : 1,
                            i.ya *= i.y > n || i.y < 0 ? -1 : 1,
                            e.fillRect(i.x - 0.5, i.y - 0.5, 1, 1);
                        for (v = 0; v < w.length; v++) {
                            x = w[v];
                            if (i !== x && null !== x.x && null !== x.y) {
                                B = i.x - x.x,
                                    z = i.y - x.y,
                                    y = B * B + z * z;
                                y < x.max && (x === f && y >= x.max / 2 && (i.x -= 0.03 * B, i.y -= 0.03 * z), A = (x.max - y) / x.max, e.beginPath(), e.lineWidth = A / 2, e.strokeStyle = "rgba(" + s.c + "," + (A + 0.2) + ")", e.moveTo(i.x, i.y), e.lineTo(x.x, x.y), e.stroke())
                            }
                        }
                        w.splice(w.indexOf(i), 1)
                    }),
                        m(b)
                }
                var u = document.createElement("canvas"),
                    s = l(),
                    c = "c_n" + s.l,
                    e = u.getContext("2d"),
                    r,
                    n,
                    m = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
                        function (i) {
                            window.setTimeout(i, 1000 / 45)
                        },
                    a = Math.random,
                    f = {
                        x: null,
                        y: null,
                        max: 20000
                    };
                u.id = c;
                u.style.cssText = "position:fixed;top:0;left:0;z-index:" + s.z + ";opacity:" + s.o;
                j("body")[0].appendChild(u);
                k(),
                    window.onresize = k;
                window.onmousemove = function (i) {
                    i = i || window.event,
                        f.x = i.clientX,
                        f.y = i.clientY
                },
                    window.onmouseout = function () {
                        f.x = null,
                            f.y = null
                    };
                for (var t = [], p = 0; s.n > p; p++) {
                    var h = a() * r,
                        g = a() * n,
                        q = 2 * a() - 1,
                        d = 2 * a() - 1;
                    t.push({
                        x: h,
                        y: g,
                        xa: q,
                        ya: d,
                        max: 6000
                    })
                }
                setTimeout(function () {
                    b()
                },
                    100)
            }();
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    enroll(this.ruleForm).then((res) => {
                        this.$message({
                            message: '账号:' + res.data,
                            type: 'success'
                        });
                        setTimeout(() => {
                            this.$router.go(-1)
                        }, 3000)
                    }).catch(err => {
                        this.$message({
                            type: 'error',
                            message: err.message,
                            showClose: true
                        })
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
    },
}
</script>
<style>
.el-icon-user-solid:before {
    font-size: 70px;
}

</style>